<?php
namespace app\wechat\controller;

use think\Controller;
class Index extends Controller{
    
    public function index2(){
        $url="https://api.haina.com/access/getToken";
        $post_data = '{  
                        "bucket_id":"hnbrdV6f9JGEB1MgD6", 
                        "bucket_secret":"mqaDe4zdor8G4HyaY9JNwnVjvN8BxgE5"
                    }';
        
       	$data=$this->curl_post($url,$post_data);
       	var_dump($data);
    }
    
    //获取小区楼栋结构列表
    public function getcommunity(){
        $agentId=$this->getAgentId();
        $token=$this->getToken();
        $url="https://api.haina.com/resources/getCommunityList?access_token={$token}&agent_id={$agentId}";
        $property_id="98181b6e4c7f954f79ce13f67583ce4f";
        $post_data = '{  
                        "property_id":"'.$property_id.'"
                    }';
        
       	$data=$this->curl_post($url,$post_data);
       	
       	$data2=json_decode($data,true);
       	echo "<pre>";
       	var_dump($data2);
    }
    
    //获取楼栋地址树
    public function getbuild(){
        $agentId=$this->getAgentId();
        $token=$this->getToken();
        $url="https://api.haina.com/resources/getCommunityAddressTree?access_token={$token}&agent_id={$agentId}";
        $property_id="98181b6e4c7f954f79ce13f67583ce4f";
        $community_id="0e1bdf33030a673dd7ef45b6522a7dca";
        $post_data = '{  
                        "property_id":"'.$property_id.'",
                        "community_id":"'.$community_id.'"
                    }';
        
       	$data=$this->curl_post($url,$post_data);
       	
       	$data2=json_decode($data,true);
       	echo "<pre>";
       	var_dump($data2);
    }
    public function userlist(){
        $code=request()->get("resident_code");
        $agentId=$this->getAgentId();
        $token=$this->getToken();
        $url=":https://api.haina.com/access/getResident?access_token={$token}&agent_id={$agentId}";
        $post_data = '{  
                        "bucket_id":"hnbrdV6f9JGEB1MgD6", 
                        "bucket_secret":"mqaDe4zdor8G4HyaY9JNwnVjvN8BxgE5"
                    }';
        
       	$data=$this->curl_post($url,$post_data);
       	var_dump($data);
    }
    public function index(){
        $resident_code=request()->get("resident_code");
        $agentId=$this->getAgentId();
        $token=$this->getToken();
        $url="https://api.haina.com/access/getResident?access_token={$token}&agent_id={$agentId}";
        $post_data = '{  
                        "resident_code":"'.$resident_code.'"
                    }';
        
       	$data=$this->curl_post($url,$post_data);
       	
       	$data2=json_decode($data,true);
       	echo "<pre>";
       	var_dump($data2);
        
    }
    public function getAgentId(){
        return "agljO7RHneOXR35bjE";
    }
    
    public function getToken(){
        return "bzJnL1pMekMrSzBiMlNNYzZ5aTROMEh0OEJLWE92dXFuSXNoMjBqS0c5WUR6RFFRdmJ0YkJhSnhIS2NSTGNrUA6ad1fd8f61833c3266d90bbec02e07146dfc15f7bdaf6153aa404f3019815cd0";
    }
    public function curl_post($url,$post_data){
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/json"));
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令
        $data = curl_exec($curl);
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            $header = substr($data, 0, $headerSize);
            $body = substr($data, $headerSize);
            curl_close($curl);
            return $body;
        }
        curl_close($curl);
        return $data;
        //关闭URL请求
        //json_decode($data, true);
	}
    
    
    public function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    } 
}

